/*
 * parseArgs.c
 *
 * Traverses an array of arguments and
 * executes the particular functions.
 * Files are stored to an array of filenames
 * and returned via *files[];
 *
 * Returns: Number of readable files.
 *
 */

#include <stddef.h> /* NULL */
#include <stdio.h> /* printf, fopen, ... */
#include <assert.h>
#include "header/parseArgs.h"

int parseArguments(const int argCount, const char* arguments[], char* files[]) {
    assert(arguments != NULL);
    assert(files != NULL);

    int i, filesLoaded = 0;
    FILE *filePointer;

    if(argCount <= 0) {
        printError(6); /* Files missing */
        return -1;
    }

    for(i = 0; i < argCount; i++) {

        if(strcmp("-h", arguments[i]) == 0) {
            PARAM_H; /* Function executed when this argument is found */
            continue;
        }

        filePointer = fopen(arguments[i], "r");
        if(filePointer == NULL) {
            printFileError(5, arguments[i]);
            continue;
        } else {
            files[filesLoaded] = arguments[i];
            ++filesLoaded;
        }

        fclose(filePointer);
    }

    return filesLoaded;
}

